package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.DifficultyActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.model.BaseModel;
import com.coollang.smartbra.model.UserGoalAndStrengthModel;

import java.util.HashMap;
import java.util.Map;

import io.realm.Realm;

/**
 * 设置训练难度的p层
 */

public class DifficultyActivityPresenter extends BasePresenter<BaseModel> {

    private DifficultyActivity mActivity;
    private int mGoal;

    public DifficultyActivityPresenter(DifficultyActivity difficultyActivity) {
        super(BaseModel.class);
        mActivity = difficultyActivity;
    }

    public void setRequest(int goal) {
        mGoal = goal;
        setRequestInfo();
    }

    @Override
    public void setRequestInfo() {
        setBaseOkgo("user/set-goal");
    }

    @Override
    protected Map<String, String> getData() {
        Map mMap = new HashMap<>();
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap.put("goal", mGoal + "");
        return mMap;
    }

    @Override
    protected void parsrData(BaseModel model) {
        if ("200".equals(model.getCode())) {
            mRealm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    UserGoalAndStrengthModel first = realm.where(UserGoalAndStrengthModel.class).findFirst();
                    first.setGoal(mGoal);
                }
            });
            mActivity.setGoalSucceed();
        }
    }
}
